package ztc.thread;

import ztc.pattern.Tools;
import ztc.tools.Net;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by ztc on 16-5-31.
 */
public class SecondThread extends Thread{
    String path;
    String url;
    int pageCount;
    public SecondThread() {
    }
    public SecondThread(String path,String url){
        this.path=path;
        this.url=url;
        pageCount= Tools.getPageCount(Net.doGet(url));
    }

    @Override
    public void run() {
        String html;
        List picUrl;
        String xflink;
        String pageUrl;
        System.out.println(pageCount);
        for(int i=1;i<=pageCount;i++){
            if(i==1){
                pageUrl=url+"index.html";
            }else{
                pageUrl=url+"index-"+i+".html";
            }
            System.out.println("分析:" + pageUrl+" 此页ing----");
            HashMap map=Tools.getPageMap(Net.doGet(pageUrl));
            Set<Map.Entry> set=map.entrySet();
            System.out.println("共:"+set.size()+" 条");
            for(Map.Entry e:set){
                System.out.println("  保存 "+e.getKey()+"("+e.getValue().toString()+") 信息" );
                html=Net.doGet(e.getValue().toString());
                picUrl=Tools.getPicUrls(html);
                xflink=Tools.getXflink(html);
                Net.saveInfo(picUrl, path + "/" + e.getKey(),xflink);
                System.out.println("  完成");
            }
        }
    }
}
